package com.base.action;

import java.io.File;
import java.io.InputStream;
import java.util.List;

import com.common.bean.Datadict;
import com.common.bean.Department;
import com.common.bean.Personnel;
import com.common.exception.SystemException;
import com.common.util.StringToken;

public class UpdatePersonnelAction extends PersonnelAbstractManagerAction {

	private static final long serialVersionUID = 5856298809065683771L;

	private Personnel personnel;

	private File file;

	private List<Department> deptlist;

	private List<Datadict> eduDict;

	private List<Datadict> sexDict;

	private List<Datadict> statusDict;

	private Datadict datadictByEdulevelcode;

	private Datadict datadictBySex;

	private Datadict datadictByStatus;

	private Department department;

	private String staffcode;
	
	private String old_password;
	
	@SuppressWarnings("unchecked")
	@Override
	public String execute() throws Exception {
		personnel = personnelService.findPersonnel(staffcode);

		if (personnel == null) {
			throw new SystemException("find personnel is null.");
		}

		datadictByEdulevelcode = personnel.getDatadictByEdulevelcode();
		datadictBySex = personnel.getDatadictBySex();
		datadictByStatus = personnel.getDatadictByStatus();
		department = personnel.getDepartment();

		deptlist = departmentService.findAllDepartment();
		eduDict = dictService.findDictDetail(StringToken.DICT_EDULEVEL);
		sexDict = dictService.findDictDetail(StringToken.DICT_SEX);
		statusDict = dictService.findDictDetail(StringToken.DICT_STATUS);
		return SUCCESS;
	}
	
	@SuppressWarnings("unchecked")
	public String goUpdateCurrLoginPersonnel() throws Exception {
		personnel = (Personnel) getSession().get(StringToken.CURRENT_LOGIN);
		if (personnel == null) {
			throw new SystemException("ERROR-PERSONNEL-01!!! no login!");
		}

		datadictByEdulevelcode = personnel.getDatadictByEdulevelcode();
		datadictBySex = personnel.getDatadictBySex();
		datadictByStatus = personnel.getDatadictByStatus();
		department = personnel.getDepartment();

		deptlist = departmentService.findAllDepartment();
		eduDict = dictService.findDictDetail(StringToken.DICT_EDULEVEL);
		sexDict = dictService.findDictDetail(StringToken.DICT_SEX);
		statusDict = dictService.findDictDetail(StringToken.DICT_STATUS);
		return SUCCESS;
	}

	public String doUpdatePersonnel() throws Exception {
		// create prams of personnel
		Personnel instance = personnelService.findPersonnel(staffcode);

		instance.setDatadictByEdulevelcode(datadictByEdulevelcode);
		instance.setDatadictBySex(datadictBySex);
		instance.setDatadictByStatus(datadictByStatus);
		instance.setDepartment(department);
		instance.setMobile(personnel.getMobile());
		instance.setPhone(personnel.getPhone());
		instance.setEmail(personnel.getEmail());
//		instance.setPassword(personnel.getPassword());
		
		personnelService.updatePersonnel(instance);
		return SUCCESS;
	}
	
	public String initPersonnelPassword() throws Exception {
		personnel = personnelService.findPersonnel(staffcode);
		
		personnel.setPassword(StringToken.INITPASSWORD);
		personnelService.updatePersonnel(personnel);
		return SUCCESS;
	}
	
	public String goUpdatePersonnelPassword() throws Exception {
		personnel =  (Personnel) getSession().get(StringToken.CURRENT_LOGIN);
		if (personnel == null) {
			throw new SystemException("ERROR-PERSONNEL-01!!! no login!");
		}
		return SUCCESS;
	}
	
	public String updatePersonnelPassword() throws Exception {
		Personnel personnel_login = (Personnel) getSession().get(StringToken.CURRENT_LOGIN);
		if (personnel_login == null) {
			throw new SystemException("ERROR-PERSONNEL-01!!! no login!");
		}else if(!personnel_login.getStaffcode().equals(personnel.getStaffcode())){
			throw new SystemException("ERROR-PERSONNEL-02!!! no permission for do this!");
		}else if(!personnel_login.getPassword().equals(old_password)) {
			throw new SystemException("ERROR-PERSONNEL-03!!! old password is wrong!");
		}
		
		Personnel instance = personnelService.findPersonnel(personnel.getStaffcode());
		
		instance.setPassword(personnel.getPassword());
		personnelService.updatePersonnel(instance);
		return SUCCESS;
	}

	public InputStream getPersonnelPhoto() {
		return null;
	}

	public List<Department> getDeptlist() {
		return deptlist;
	}

	public void setDeptlist(List<Department> deptlist) {
		this.deptlist = deptlist;
	}

	public Personnel getPersonnel() {
		return personnel;
	}

	public void setPersonnel(Personnel personnel) {
		this.personnel = personnel;
	}

	public Datadict getDatadictByEdulevelcode() {
		return datadictByEdulevelcode;
	}

	public void setDatadictByEdulevelcode(Datadict datadictByEdulevelcode) {
		this.datadictByEdulevelcode = datadictByEdulevelcode;
	}

	public Datadict getDatadictBySex() {
		return datadictBySex;
	}

	public void setDatadictBySex(Datadict datadictBySex) {
		this.datadictBySex = datadictBySex;
	}

	public Datadict getDatadictByStatus() {
		return datadictByStatus;
	}

	public void setDatadictByStatus(Datadict datadictByStatus) {
		this.datadictByStatus = datadictByStatus;
	}

	public Department getDepartment() {
		return department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

	public List<Datadict> getEduDict() {
		return eduDict;
	}

	public void setEduDict(List<Datadict> eduDict) {
		this.eduDict = eduDict;
	}

	public List<Datadict> getSexDict() {
		return sexDict;
	}

	public void setSexDict(List<Datadict> sexDict) {
		this.sexDict = sexDict;
	}

	public List<Datadict> getStatusDict() {
		return statusDict;
	}

	public void setStatusDict(List<Datadict> statusDict) {
		this.statusDict = statusDict;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getStaffcode() {
		return staffcode;
	}

	public void setStaffcode(String staffcode) {
		this.staffcode = staffcode;
	}

	public String getOld_password() {
		return old_password;
	}

	public void setOld_password(String old_password) {
		this.old_password = old_password;
	}


}
